/*
 * @Author: hc
 * @Date: 2024-03-18 15:58:15
 * @LastEditTime: 2024-03-28 09:55:26
 * @LastEditors: hc
 * @Description:
 */
import type { TabsProps } from 'antd';
import { useCallback, useMemo } from 'react';
import { AsyncTree, Tabs } from '../../../../functionalComponent';
import { Layout } from '../../../../util';
import { getExpressionFieldTree } from '../../service';
import FunctionCtx from '../functionCtx';

export default function ({ editor, setCurrentFunc, sendParams }) {
  const getBizTreeMemo = useCallback(() => {
    return () => {
      return getExpressionFieldTree(sendParams);
    };
  }, [sendParams]);

  const onSelect = (_, { node }) => {
    editor.current?.insertEdit?.({ text: node.expressionDisplay, type: 'field' });
  };
  const items: TabsProps['items'] = useMemo(() => {
    return [
      {
        key: '1',
        label: '函数',
        children: <FunctionCtx editor={editor} setCurrentFunc={setCurrentFunc} />
      },
      {
        key: '2',
        label: '字段',
        children: (
          <Layout.Flex style={{ flex: 1, height: '100%' }}>
            <AsyncTree
              virtual={false}
              request={getBizTreeMemo()}
              onSelect={onSelect}
              icon={false}
              convertNode={(node: any) => {
                return {
                  ...node,
                  key: node.key,
                  title: node.title,
                  isLeaf: node.leaf
                };
              }}
            />
          </Layout.Flex>
        )
      }
    ];
  }, [editor, getBizTreeMemo]);

  return <Tabs defaultActiveKey="1" fitHeight items={items} />;
}
